package com.xingkeduo.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.gateway.authority.domain.ManagerDomain;
import com.xingkeduo.gateway.authority.pojo.Token;
import com.xingkeduo.util.pojo.exception.AuthorityException;
import com.xingkeduo.util.tool.ReturnUtil;

import reactor.core.publisher.Mono;

@Configuration
public class ModifyResponseBodyConfig {
	@Autowired
	private ManagerDomain managerDomain;

	@Bean
	public RouteLocator routes(RouteLocatorBuilder routeLocatorBuilder, ObjectMapper objectMapper) {
		return routeLocatorBuilder.routes().route("login_after_route", r -> r.path("/login/**")
				.filters(f -> f.modifyResponseBody(String.class, String.class, (exchange, s) -> {
					try {
						System.out.println(s);
						JsonNode root = ReturnUtil.analyze(s);
						if (root.path("status").asInt() == 1) {

							Token token = new Token().setUid(root.path("data").path("id").asText());
							managerDomain.newAuthority(token);

							ObjectNode node = (ObjectNode) root;
							ObjectNode data = (ObjectNode) root.path("data");
							data.put("token", token.getAccessToken());
							return Mono.just(node.toString());
						}
					} catch (Throwable e) {
						e.printStackTrace();
					}
					return Mono.just(s);
				})).uri("lb://BUSINESS-USER")).route("op_login_after_route", r -> r.path("/cms/op/login/**")
						.filters(f -> f.modifyResponseBody(String.class, String.class, (exchange, s) -> {
							try {
								System.out.println(s);
								JsonNode root = ReturnUtil.analyze(s);
								if (root.path("status").asInt() == 1) {
									Token token = new Token().setUid(root.path("data").path("id").asText());
									managerDomain.newAuthority(token);

									ObjectNode node = (ObjectNode) root;
									ObjectNode data = (ObjectNode) root.path("data");
									data.put("token", token.getAccessToken());

									return Mono.just(node.toString());
								}
							} catch (Throwable e) {
								e.printStackTrace();
								return Mono.just(new ReturnUtil().getException(AuthorityException.getDefaultCode(),
										e.getMessage()));
							}
							return Mono.just(s);
						})).uri("lb://BUSINESS-OPERATION"))
				.build();
	}

	private Mono<String> rewrite(String requestBody) throws Throwable {
		try {
			// System.out.println(requestBody);
			JsonNode root = ReturnUtil.analyze(requestBody);
			if (root.path("status").asInt() == 1) {

				Token token = new Token().setUid(root.path("id").asText());
				managerDomain.newAuthority(token);

				ObjectNode node = (ObjectNode) root;
				ObjectNode data = (ObjectNode) root.path("data");
				data.put("token", token.getAccessToken());

				return Mono.just(node.toString());
			}
		} catch (Throwable e) {
			e.printStackTrace();
			return Mono.just(new ReturnUtil().getException(AuthorityException.getDefaultCode(), e.getMessage()));
		}
		return Mono.just(requestBody);
	}
}
